/**
 * 
 */
package com.uit.trip_.location;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.Point;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.location.Geocoder;
import android.util.Log;
import android.widget.Toast;

import com.google.android.maps.GeoPoint;
import com.google.android.maps.MapView;
import com.google.android.maps.OverlayItem;
import com.google.android.maps.Projection;

/**
 * @author duongnguyen
 * 
 */
public class MyLocation extends BalloonItemizedOverlay<OverlayItem> {

	public static final String MYADDRESS = "address";
	GeoPoint geo;
	private ArrayList<OverlayItem> mOverlays = new ArrayList<OverlayItem>();
	private Context context;
	int distance;
	MapView mapView;

	// private Context c;
	public MyLocation(Drawable defaultMarker, GeoPoint geo, MapView mapView) {
		super(boundCenter(defaultMarker), mapView);
		// TODO Auto-generated constructor stub
		// this.c = mapView.getContext();
		this.geo = geo;
		distance = 1000;
		this.mapView = mapView;
	}

	@Override
	public boolean draw(final Canvas canvas, MapView mapView, boolean shadow,
			long when) {
		// TODO Auto-generated method stub
		super.draw(canvas, mapView, shadow, when);
		context = mapView.getContext();
		int level = mapView.getZoomLevel();
		int index = getRealZoom(level);
		// Log.i("duongnx", "level::"+level);
		Projection project = mapView.getProjection();
		final Point point = new Point();
		project.toPixels(geo, point);
		final Paint paint = new Paint();
		paint.setStrokeWidth(0);
		paint.setStyle(Style.FILL);
		paint.setAntiAlias(true);
		paint.setColor(Color.BLUE);
		paint.setAlpha(20);
		canvas.drawCircle(point.x, point.y, index * getDistance(distance),
				paint);

		paint.setStyle(Style.STROKE);
		paint.setColor(Color.BLUE);
		canvas.drawCircle(point.x, point.y, index * getDistance(distance),
				paint);

		return true;
	}

	// public String saveBitmap() {
	// Bitmap bitmap;
	// View v = (View) mapView.getParent();
	// v.setDrawingCacheEnabled(true);
	// bitmap = v.getDrawingCache();
	//
	// String resultPath = SendImageActivity.getOutputFile();
	//
	// if (bitmap != null && !resultPath.equalsIgnoreCase("")) {
	// ProcessImage.getInstance(mapView.getContext()).writeBitmapToFile(
	// bitmap, resultPath, "png");
	// v.setDrawingCacheEnabled(false);
	// return resultPath;
	// }
	// v.setDrawingCacheEnabled(false);
	// return "";
	// }

	public void addOverlay(OverlayItem item) {
		mOverlays.add(item);
		populate();
	}

	@Override
	protected OverlayItem createItem(int i) {
		// TODO Auto-generated method stub
		return mOverlays.get(i);
	}

	@Override
	public int size() {
		// TODO Auto-generated method stub
		return mOverlays.size();
	}

	@Override
	protected boolean onBalloonTap(int index, OverlayItem item) {
		// TODO Auto-generated method stub
		String address = getMyAddress();
		// String path = saveBitmap();
		// Intent i = new Intent(context, MyAccountActivity.class);
		// if (address != null)
		// i.putExtra(MYADDRESS, address);
		// i.putExtra("MapPath", path);
		// context.startActivity(i);
		Toast.makeText(context, "MyAddress is::" + address, Toast.LENGTH_SHORT)
				.show();
		
		return super.onBalloonTap(index, item);
	}

	private String getMyAddress() {
		StringBuilder myAddress = new StringBuilder();
		Double la = (geo.getLatitudeE6() / (1E6));
		Double lng = (geo.getLongitudeE6() / (1E6));
		Geocoder gc = new Geocoder(context, Locale.getDefault());
		List<Address> addresses = new ArrayList<Address>();
		try {
			addresses = gc.getFromLocation(la, lng, 5);
		} catch (Exception e) {
			// TODO: handle exception
			Log.e("duongnx", "error:" + e);
		}

		if (addresses.size() != 0) {
			Address address = addresses.get(0);
			for (int i = 0; i < address.getMaxAddressLineIndex(); i++) {
				if (i != address.getMaxAddressLineIndex() - 1) {
					myAddress.append(address.getAddressLine(i)).append(", ");
				} else {
					myAddress.append(address.getAddressLine(i));
				}
			}
		}
		if (myAddress == null || myAddress.equals("")) {
			return "Not found";
		}
		return myAddress.toString();
	}

	private int getRealZoom(int level) {
		int index = 1;
		switch (level) {

		case 6:
		case 7:
		case 8:
		case 9:
		case 10:
		case 11:
			index = 1;
			break;
		case 12:
		case 13:
		case 14:
			index = 2;
			break;
		case 15:
			index = 3;
			break;
		case 16:
			index = 5;
			break;
		case 17:
		case 18:
		case 19:
			index = 7;
			break;
		default:
			index = 0;
			break;
		}
		return index;
	}

	private int getDistance(int d) {
		if (d >= 1000 && d < 2000)
			return 25;
		if (d >= 2000 && d < 3000)
			return 30;
		if (d > 3000)
			return 35;
		return 15;
	}
}
